layui.define(['jquery'], function (exports) {
    var $ = layui.jquery;
    var http = {
        get: function (request) {
            request.method = 'get';
            return send(request);
        },
        post: function (request) {
            request.method = 'post';
            return send(request);
        },
        delete: function (request) {
            request.method = 'delete';
            return send(request);
        },
        put: function (request) {
            request.method = 'put';
            return send(request);
        },
        info: function (request) {
            request.method = 'info';
            return send(request);
        }
    }

    function send(request) {
        var url = constant.server.ip + request.url
        if (request.url.indexOf('.json') != -1) {
            url = window.location + request.url;
        }
        return $.ajax({
            type: request.method,
            url: url,
            data: ajaxEmpty(request.data),
            async: typeof(request.async) == 'undefined' ? 'true' : request.async,
            contentType: "json",
            cache: false,
            headers: header,
            dataType: 'json',
            beforeSend: function (xhr) {
                if (typeof(request.cl) == 'undefined') {
                }
            },
            statusCode: {
                200: function () {
                    kit.progress.close();
                }
            },
            error: function (e) {
                kit.progress.close();
                var res = e.responseJSON;
                var status = e.status;
                //console.log(e)
                switch (status) {
                    case 401:
                        component.tips.error(res.message);
                        break;
                    case 403:
                        component.tips.warning('您没有权限进行该操作！');
                        break;
                    case 406:
                        component.tips.error(res.message);
                        /*setTimeout(sys.exit(), 3000);*/
                        break;
                    case 411:
                        component.tips.error(res.message);
                        /* setTimeout(sys.exit(), 3000);*/
                        break;
                    default:
                        try {
                            component.tips.error(res.message);
                        } catch (e) {
                            component.tips.error('服务器异常通讯失败');
                        }
                }
            }
        });
    }

    function ajaxEmpty(value) {
        if (typeof(value) == "undefined")
            return "";
        if (typeof(value) == "object")
            return JSON.stringify(value);
        return value;
    }

    exports('http', http);
});